package com.gusto.engine.semsim.util;

import org.apache.log4j.Logger;

import com.gusto.engine.beans.exceptions.PropertyAccessException;
import com.gusto.engine.beans.model.Res;
import com.gusto.engine.semsim.exceptions.SimilarityException;
import com.gusto.engine.semsim.measures.ResourceSimilarity;
import com.gusto.engine.storage.persistence.PersistencePlugin;

public class SimilarityUtils {
	
	private Logger log = Logger.getLogger(SimilarityUtils.class);
	
	boolean enableFromPersisted = false;
	
	private Double getSimilarityPersistencePlugin(String context, Res resource1, Res resource2, PersistencePlugin persistencePlugin) throws SimilarityException {
		Double plugingCachedSim = null;
		try {
			if (persistencePlugin != null) {
				plugingCachedSim = persistencePlugin.getDistance(
						context, 
						//urlToId.transformURLToId(resource1.toString()), 
						//urlToId.transformURLToId(resource2.toString()));
						resource1.toString(), 
						resource2.toString());
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return plugingCachedSim;
	}
	
	private Double getSimilaritySemantic(String context, Res resource1, Res resource2, ResourceSimilarity resSim) throws SimilarityException, PropertyAccessException {
		//ResourceSimilarity resSim = resourceSimilarityMap.get(context);
		Double sim = resSim.getSimilarity(resource1, resource2);
		//Double sim = 0.99;
		return sim;
	}
	
	/**
	 * Only method that allows calculation of distances, 
	 * so that it can include plugin's for storing, sending, the information
	 * @param context
	 * @param resource1
	 * @param resource2
	 * @return
	 * @throws SimilarityException
	 */
	public Double getSimilarity(String context, Res resource1, Res resource2, 
			                    PersistencePlugin persistencePlugin, ResourceSimilarity resSim) 
								throws SimilarityException, PropertyAccessException {
		if (enableFromPersisted) {
			Double plugingCachedSim = getSimilarityPersistencePlugin(context, resource1, resource2, persistencePlugin);
			if (plugingCachedSim != null) {
				log.debug("Similarity from persistence '" + context + "' " + resource1 + "-" + resource2 + " ");
				log.info("+Similarity+ " + plugingCachedSim + " '" + context + "' " + resource1 + "-" + resource2);
				return plugingCachedSim;
			}
		}
		
		// If there's no value loaded from persistence, calculate semantic (no db cache)
		Double sim = getSimilaritySemantic(context, resource1, resource2, resSim);
		//Double sim = 0.1;
		log.debug("Similarity recalculated from semantic '" + context + "' " + resource1 + "-" + resource2 + "");
		
		// Update distance in the persistence Plugin
		if (persistencePlugin != null) {
			/*
			String id1 = urlToId.transformURLToId(resource1.toString());
			String id2 = urlToId.transformURLToId(resource2.toString());
			*/
			String id1 = resource1.toString();
			String id2 = resource2.toString();
			persistencePlugin.updateDistance(context, ""+id1 , ""+id2, sim);
		}
		
		log.info("+Similarity+ " + sim + " '" + context + "' " + resource1 + "-" + resource2);
		return sim;
	}
	
	
	
}
